
import bpy







def f_getObject曲线(obj, path, 通道, groupName=""):
	if obj.animation_data == None : 
		obj.animation_data_create()
		obj.animation_data.action = bpy.data.actions.new(obj.name+'Action')
		#曲线 = obj.animation_data.action.fcurves.new(path, index=通道, action_group=path)
	f = None
	#print('obj.animation_data.action.fcurves = ', obj.animation_data.action.fcurves)
	for 曲线 in obj.animation_data.action.fcurves :
		print('曲线 = ', 曲线)
		if 曲线.data_path == path and 曲线.array_index == 通道:
			f = 曲线
			break
	if f == None :
		f = obj.animation_data.action.fcurves.new(path, index=通道, action_group=groupName)
	return f





def f_getObject曲线s(obj, paths, 通道=-1):
	if obj.animation_data == None : 
		obj.animation_data_create()
		obj.animation_data.action = bpy.data.actions.new(obj.name+'Action')
		#print('fcurves 创建曲线 = ', obj.animation_data.action)
	if obj.animation_data.action == None :
		obj.animation_data.action = bpy.data.actions.new(obj.name+'Action')

	f = []
	
	if obj.animation_data.action != None :
		for 曲线 in obj.animation_data.action.fcurves :
			for path in paths:
				#print('曲线.data_path = ', 曲线.data_path, path)
				if 曲线.data_path == path :
					if 通道 >= 0:
						if 曲线.array_index == 通道 :
							f.append(曲线)
					else:
						f.append(曲线)
					break
	#print('fcurves = ', obj.animation_data.action, obj, paths, f)
	return f



'''def f_remove对象关键点(曲线, 帧=-1):
	if 帧 >= 0 :
		for 关键点 in 曲线.keyframe_points :
			if 关键点.co[0] == 帧 :
				曲线.keyframe_points.remove(关键点)
				break
	else:
		while len(曲线.keyframe_points) :
			关键点 = 曲线.keyframe_points[0]
			曲线.keyframe_points.remove(关键点)
			曲线.update()

def f_删除指定关键帧(曲线, 帧):
	for 关键点 in 曲线.keyframe_points :
		if 关键点.co[0] == 帧 :
			曲线.keyframe_points.remove(关键点)
			break
def f_删除所有曲线(fcurves):
	while len(fcurves) :
		fcurves.remove(fcurves[0])
def f_remove对象PATH曲线(fcurves, paths):
	index = 0
	while len(fcurves) :
		f = fcurves[index]
		是否递加 = True
		for p in paths:
			if f.data_path == p :
				fcurves.remove(f)
				是否递加 = False
				break
		if 是否递加 : index += 1'''



def f_删除指定关键帧(obj, 帧, paths, 通道=-1):
	fcurves = f_getObject曲线s(obj, paths, 通道)

	for 曲线 in fcurves:
		for 关键点 in 曲线.keyframe_points :
			if 关键点.co[0] == 帧 :
				曲线.keyframe_points.remove(关键点)
				break


def f_删除所有关键点(曲线):
	while len(曲线.keyframe_points) :
		关键点 = 曲线.keyframe_points[0]
		曲线.keyframe_points.remove(关键点)
		曲线.update()


def f_删除指定路径曲线的所有关键点(obj, paths=[], 通道=-1):
	fcurves = []
	fcurves.extend(f_getObject曲线s(obj, paths, 通道))
	#print('fcurves = ', fcurves, obj, paths)
	for f in fcurves:
		f_删除所有关键点(f)


def f_删除指定路径所有曲线(obj, paths=[], 通道=-1):
	print('** f_删除指定路径所有曲线 ** ')
	if obj.animation_data != None : 
		obj.animation_data_clear()
	return


'''def f_数据插帧(obj, path, 组名称, 帧, 值, 通道=0):
	fs = f_getObject曲线(obj, path, 通道)
	
	if fs == None:
		fs = obj.animation_data.action.fcurves.new(path, index=通道, action_group=组名称)
	
	#print('obj.animation_data = ', fs, 帧, 值)
	fs.keyframe_points.insert(帧, 值)
	fs.keyframe_points[-1].interpolation = 'BEZIER'
	return fs'''


 


def f_数据插帧(obj, path, 组名称, 帧, 值, 通道=0):
	fs = f_getObject曲线(obj, path, 通道, 组名称)
	
	fs.keyframe_points.insert(帧, 值)
	fs.keyframe_points[-1].interpolation = 'BEZIER'
	return fs
